package businessLogic.transaction;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 *
 * @author Alexander
 */
public class JPATransaction implements Transaction<EntityManager> {

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU");
    private EntityManager em;
    private EntityTransaction tx;

    public JPATransaction() {
    }
    

    @Override
    public boolean createTransaction() {
        try {
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public void commit() {
        tx.commit();
    }

    @Override
    public void close() {
        em.clear();
        em.close();
    }

    @Override
    public void rollBack() {
        tx.rollback();
    }

    @Override
    public EntityManager getTransaction() {
        return em;
    }
}
